package com.techwells.taco.widget;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;

import com.techwells.heartfeltgift.R;



/**
 * 
 * Create custom Dialog windows for your application Custom dialogs rely on
 * custom layouts wich allow you to create and use your own look & feel.
 * 
 * Under GPL v3 : http://www.gnu.org/licenses/gpl-3.0.html
 * 
 * @author antoine vianey
 * 
 */
public class CustomProgressDialog extends Dialog {

	public CustomProgressDialog(Context context) {
		super(context);

	}

	public CustomProgressDialog(Context context, int dialog) {
		super(context, dialog);
	}

	/**
	 * Helper class for creating a custom dialog
	 */
	public static class Builder {
		private Activity activity;
		public ImageView loding_bg;

		public Builder(Activity activity) {
			this.activity = activity;
		}

		/**
		 * Create the custom dialog
		 */
		public CustomProgressDialog create() {
			final CustomProgressDialog dialog = new CustomProgressDialog(
					activity, R.style.Dialog);
			LayoutInflater inflater = LayoutInflater.from(activity);
			View layout = inflater.inflate(
					R.layout.common_custom_progress_dialog, null);
			ImageView progressBar = ((ImageView) layout
					.findViewById(R.id.loading_progressBar));
//			AnimationDrawable animationDrawable = (AnimationDrawable) progressBar
//					.getBackground();
//			animationDrawable.start();
			/*loding_bg = (ImageView) layout.findViewById(R.id.loding_bg);
			startRotateAnimation(loding_bg);*/
			dialog.addContentView(layout, new LayoutParams(
					LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
			/*dialog.setOnCancelListener(new OnCancelListener() {
				
				@Override
				public void onCancel(DialogInterface dialog) {
					Animation animation = loding_bg.getAnimation();
					loding_bg.clearAnimation();
					Log.e("art", "cancel");
				}
			});*/
			return dialog;
		}
		
		//旋转动画
		private void startRotateAnimation(final View view) {
			RotateAnimation rotateAnimation = new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF, 0.5f,
					Animation.RELATIVE_TO_SELF, 0.5f);
			rotateAnimation.setDuration(3000);
			rotateAnimation.setRepeatCount(-1);
			LinearInterpolator lin = new LinearInterpolator();  
			rotateAnimation.setInterpolator(lin);  
			view.startAnimation(rotateAnimation);
		}

	}

}